<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>Socket基本操作的C&#43;&#43;封装 - L_B__</title><meta name="referrer" content="no-referrer">
<meta name="description" content="”Socket基本操作的C&#43;&#43;封装“"><meta property="og:title" content="Socket基本操作的C&#43;&#43;封装" />
<meta property="og:description" content="”Socket基本操作的C&#43;&#43;封装“" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" /><meta property="og:image" content="https://acking-you.github.io/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2022-01-20T00:00:00+00:00" />
<meta property="article:modified_time" content="2022-01-20T00:00:00+00:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://acking-you.github.io/logo.png"/>

<meta name="twitter:title" content="Socket基本操作的C&#43;&#43;封装"/>
<meta name="twitter:description" content="”Socket基本操作的C&#43;&#43;封装“"/>
<meta name="application-name" content="FeelIt">
<meta name="apple-mobile-web-app-title" content="FeelIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="canonical" href="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" /><link rel="prev" href="https://acking-you.github.io/posts/%E5%A4%A7%E6%95%B4%E6%95%B0%E5%8A%A0%E5%87%8F/" /><link rel="next" href="https://acking-you.github.io/posts/c&#43;&#43;%E7%9A%84%E6%A8%A1%E6%9D%BF%E5%86%99println/" /><link rel="stylesheet" href="/css/page.min.css"><link rel="stylesheet" href="/css/home.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Socket基本操作的C++封装",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/acking-you.github.io\/posts\/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc\u002b\u002b%E5%B0%81%E8%A3%85\/"
        },"genre": "posts","keywords": "socket通信","wordcount":  2682 ,
        "url": "https:\/\/acking-you.github.io\/posts\/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc\u002b\u002b%E5%B0%81%E8%A3%85\/","datePublished": "2022-01-20T00:00:00+00:00","dateModified": "2022-01-20T00:00:00+00:00","publisher": {
            "@type": "Organization",
            "name": "作者"},"author": {
                "@type": "Person",
                "name": "作者"
            },"description": "”Socket基本操作的C++封装“"
    }
    </script></head><body data-header-desktop="auto" data-header-mobile="auto"><script>(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="L_B__">L_B__</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="#" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="L_B__">L_B__</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="#" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="#" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><div class="menu-item"><a href="javascript:void(0);" class="theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div></div>
    </div>
</header><div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single" data-toc="disable"><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/img_convert/c3d009d6542a44155b5847c3a9c76dcd.png#pic_center"
        data-srcset="https://img-blog.csdnimg.cn/img_convert/c3d009d6542a44155b5847c3a9c76dcd.png#pic_center, https://img-blog.csdnimg.cn/img_convert/c3d009d6542a44155b5847c3a9c76dcd.png#pic_center 1.5x, https://img-blog.csdnimg.cn/img_convert/c3d009d6542a44155b5847c3a9c76dcd.png#pic_center 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/img_convert/c3d009d6542a44155b5847c3a9c76dcd.png#pic_center"
        title="”Socket基本操作的C&#43;&#43;封装“" /></div><div class="single-card" data-image="true"><h2 class="single-title animated flipInX">Socket基本操作的C&#43;&#43;封装</h2><div class="post-meta">
                <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>作者</a></span>&nbsp;<span class="post-category">出版于  <a href="/categories/c++%E5%AE%9E%E6%88%98/"><i class="far fa-folder fa-fw"></i>C++实战</a></span></div>
                <div class="post-meta-line"><span><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2022-01-20">2022-01-20</time></span>&nbsp;<span><i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 2682 字</span>&nbsp;
                    <span><i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 6 分钟</span>&nbsp;</div>
            </div>
            
            <hr><div class="details toc" id="toc-static"  data-kept="">
                    <div class="details-summary toc-title">
                        <span>目录</span>
                        <span><i class="details-icon fas fa-angle-right"></i></span>
                    </div>
                    <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#封装过程">封装过程</a>
      <ul>
        <li><a href="#接口类的实现抽象类">接口类的实现(抽象类)</a></li>
        <li><a href="#服务器端封装">服务器端封装</a></li>
        <li><a href="#客户端的封装">客户端的封装</a></li>
      </ul>
    </li>
    <li><a href="#实例讲解">实例讲解</a>
      <ul>
        <li><a href="#实例一回声程序通信">实例一：回声程序通信</a></li>
        <li><a href="#实例二文件操作传送图片掌握重复传包">实例二：文件操作，传送图片(掌握重复传包)</a></li>
      </ul>
    </li>
    <li><a href="#实例三web通信浏览器访问服务器">实例三：Web通信(浏览器访问服务器)</a>
      <ul>
        <li><a href="#web服务器程序">Web服务器程序</a></li>
        <li><a href="#接收结果-1">接收结果</a></li>
      </ul>
    </li>
    <li><a href="#总结">总结</a></li>
  </ul>
</nav></div>
                </div><div class="content" id="content"><h2 id="封装过程">封装过程</h2>
<h3 id="接口类的实现抽象类">接口类的实现(抽象类)</h3>
<h4 id="_public_socketh">_public_socket.h</h4>
<blockquote>
<p>该头文件用于包含所有该系统平台socket所需要依赖的库。</p>
</blockquote>
<ul>
<li>windows平台</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#ifndef MY_TINY_STL__PUBLIC_SOCKET_H
</span><span class="cp">#define MY_TINY_STL__PUBLIC_SOCKET_H
</span><span class="cp">#include</span> <span class="cpf">&lt;winsock2.h&gt;</span><span class="cp">
</span><span class="cp">#pragma comment (lib, &#34;ws2_32.lib&#34;)  </span><span class="c1">//加载 ws2_32.dll
</span><span class="c1"></span><span class="cp">#endif </span><span class="c1">//MY_TINY_STL__PUBLIC_SOCKET_H
</span></code></pre></div><ul>
<li>Linux平台</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;unistd.h&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;arpa/inet.h&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;sys/socket.h&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;netinet/in.h&gt;</span><span class="cp">
</span></code></pre></div><h4 id="tcp_interfaceh作用于win平台">TCP_INTERFACE.h(作用于win平台)</h4>
<blockquote>
<p>由于该接口由服务器端和客户端继承，而两者同样的函数成员也就是这些了，设计客户端和服务器端时就只需要考虑各自的套接字以及其余操作的成员函数，也不需要管理DLL的开关。
还有一个erro_die()成员函数用于阻断错误并打印对应情况。</p>
</blockquote>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="c1">//
</span><span class="c1">// Created by Alone on 2021/8/17.
</span><span class="c1">//
</span><span class="c1"></span>
<span class="cp">#ifndef MY_TINY_STL_TCP_INTERFACE_H
</span><span class="cp">#define MY_TINY_STL_TCP_INTERFACE_H
</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;cstdio&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&#34;_public_socket.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="k">class</span> <span class="nc">TCP_INTERFACE</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
    <span class="n">TCP_INTERFACE</span><span class="p">()</span> <span class="p">{</span>
        <span class="c1">//初始化 DLL
</span><span class="c1"></span>        <span class="n">WSADATA</span> <span class="n">wsaData</span><span class="p">;</span>
        <span class="n">WSAStartup</span><span class="p">(</span><span class="n">MAKEWORD</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="o">&amp;</span><span class="n">wsaData</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">//返回值小于等于0时发生错误
</span><span class="c1"></span>    <span class="k">virtual</span> <span class="kt">int</span> <span class="nf">Send</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">buflen</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="k">virtual</span> <span class="kt">int</span> <span class="nf">Recv</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">buflen</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="c1">//closesocket返回值不为0则发生错误
</span><span class="c1"></span>    <span class="k">virtual</span> <span class="kt">void</span> <span class="nf">Close</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="k">virtual</span> <span class="kt">void</span> <span class="nf">error_die</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">)</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="o">~</span><span class="n">TCP_INTERFACE</span><span class="p">()</span> <span class="p">{</span>
        <span class="n">WSACleanup</span><span class="p">();</span>
    <span class="p">}</span>

<span class="p">};</span>


<span class="cp">#endif </span><span class="c1">//MY_TINY_STL_TCP_INTERFACE_H
</span><span class="c1"></span>
</code></pre></div><h3 id="服务器端封装">服务器端封装</h3>
<blockquote>
<p>这次修改了下逻辑，还是用accept返回一个套接字进行发送和接收操作比较好。类的底层没有再保留用于和某个客户端通信的套接字了，自己控制各个客户端套接字的关闭和使用。</p>
</blockquote>
<h4 id="tcp_socket_serverh">TCP_SOCKET_SERVER.h</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="c1">//
</span><span class="c1">// Created by Alone on 2021/8/16.
</span><span class="c1">//
</span><span class="c1"></span>
<span class="cp">#ifndef MY_TINY_STL_TCP_SOCKET_SERVER_H
</span><span class="cp">#define MY_TINY_STL_TCP_SOCKET_SERVER_H
</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&#34;TCP_INTERFACE.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="k">class</span> <span class="nc">TCP_SOCKET_SERVER</span> <span class="o">:</span> <span class="k">public</span> <span class="n">TCP_INTERFACE</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
    <span class="n">TCP_SOCKET_SERVER</span><span class="p">();</span>

    <span class="o">~</span><span class="n">TCP_SOCKET_SERVER</span><span class="p">();</span>

    <span class="kt">void</span> <span class="nf">Bind</span><span class="p">(</span><span class="kt">int</span> <span class="n">port</span><span class="p">);</span>

    <span class="kt">void</span> <span class="nf">Listen</span><span class="p">();</span>

    <span class="n">SOCKET</span> <span class="nf">Accept</span><span class="p">();</span>

    <span class="kt">int</span> <span class="nf">Send</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">buflen</span><span class="p">);</span>

    <span class="kt">int</span> <span class="nf">Recv</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">buflen</span><span class="p">);</span>

    <span class="kt">void</span> <span class="nf">Close</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">);</span>

    <span class="kt">void</span> <span class="nf">error_die</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">);</span>

<span class="k">private</span><span class="o">:</span>
    <span class="n">SOCKET</span> <span class="n">servSock</span><span class="p">;</span>
    <span class="n">sockaddr_in</span> <span class="n">sockAddr</span><span class="p">;</span>

<span class="p">};</span>


<span class="cp">#endif </span><span class="c1">//MY_TINY_STL_TCP_SOCKET_SERVER_H
</span><span class="c1"></span>
</code></pre></div><h4 id="tcp_socket_servercpp">TCP_SOCKET_SERVER.cpp</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="c1">//
</span><span class="c1">// Created by Alone on 2021/8/16.
</span><span class="c1">//
</span><span class="c1"></span>
<span class="cp">#include</span> <span class="cpf">&#34;TCP_SOCKET_SERVER.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="c1">//初始化操作
</span><span class="c1"></span><span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">TCP_SOCKET_SERVER</span><span class="p">()</span> <span class="o">:</span> <span class="n">servSock</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sockAddr</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">sockAddr</span><span class="p">));</span>  <span class="c1">//每个字节都用0填充
</span><span class="c1"></span><span class="p">}</span>

<span class="c1">//绑定操作
</span><span class="c1"></span><span class="kt">void</span> <span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">Bind</span><span class="p">(</span><span class="kt">int</span> <span class="n">port</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">servSock</span> <span class="o">=</span> <span class="n">socket</span><span class="p">(</span><span class="n">PF_INET</span><span class="p">,</span> <span class="n">SOCK_STREAM</span><span class="p">,</span> <span class="n">IPPROTO_TCP</span><span class="p">);</span>
    <span class="n">sockAddr</span><span class="p">.</span><span class="n">sin_family</span> <span class="o">=</span> <span class="n">PF_INET</span><span class="p">;</span>  <span class="c1">//使用IPv4地址
</span><span class="c1"></span>    <span class="n">sockAddr</span><span class="p">.</span><span class="n">sin_addr</span><span class="p">.</span><span class="n">s_addr</span> <span class="o">=</span> <span class="n">htonl</span><span class="p">(</span><span class="n">INADDR_ANY</span><span class="p">);</span>  <span class="c1">//具体的IP地址
</span><span class="c1"></span>    <span class="n">sockAddr</span><span class="p">.</span><span class="n">sin_port</span> <span class="o">=</span> <span class="n">htons</span><span class="p">(</span><span class="n">port</span><span class="p">);</span>  <span class="c1">//端口
</span><span class="c1"></span>    <span class="k">if</span> <span class="p">(</span><span class="n">bind</span><span class="p">(</span><span class="n">servSock</span><span class="p">,</span> <span class="p">(</span><span class="n">SOCKADDR</span> <span class="o">*</span><span class="p">)</span> <span class="o">&amp;</span><span class="n">sockAddr</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">SOCKADDR</span><span class="p">))</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">error_die</span><span class="p">(</span><span class="s">&#34;bind&#34;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//置于监听状态
</span><span class="c1"></span><span class="kt">void</span> <span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">Listen</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">servSock</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">error_die</span><span class="p">(</span><span class="s">&#34;listen&#34;</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">listen</span><span class="p">(</span><span class="n">servSock</span><span class="p">,</span> <span class="n">SOMAXCONN</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">error_die</span><span class="p">(</span><span class="s">&#34;listen&#34;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//利用套接字的监听串口，接收客户端的请求，建立新的套接字进行存储信息
</span><span class="c1"></span><span class="n">SOCKET</span> <span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">Accept</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">SOCKADDR</span> <span class="n">t</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">nSize</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">SOCKADDR</span><span class="p">);</span>
    <span class="c1">//后面两个参数为可选
</span><span class="c1"></span>    <span class="n">SOCKET</span> <span class="n">clnt</span> <span class="o">=</span> <span class="n">accept</span><span class="p">(</span><span class="n">servSock</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">t</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">nSize</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">clnt</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span><span class="n">error_die</span><span class="p">(</span><span class="s">&#34;accept&#34;</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">clnt</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//返回的是发送到缓冲区的字节长度
</span><span class="c1"></span><span class="kt">int</span> <span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">Send</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">buflen</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nf">send</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span> <span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">buf</span><span class="p">,</span> <span class="n">buflen</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">//返回已经接收的字节长度
</span><span class="c1"></span><span class="kt">int</span> <span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">Recv</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">buflen</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nf">recv</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">buf</span><span class="p">,</span> <span class="n">buflen</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">//析构函数关闭socket
</span><span class="c1"></span><span class="n">TCP_SOCKET_SERVER</span><span class="o">::~</span><span class="n">TCP_SOCKET_SERVER</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">servSock</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span><span class="n">closesocket</span><span class="p">(</span><span class="n">servSock</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">Close</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">closesocket</span><span class="p">(</span><span class="n">clnt</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">error_die</span><span class="p">(</span><span class="s">&#34;closesocket&#34;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">TCP_SOCKET_SERVER</span><span class="o">::</span><span class="n">error_die</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;[hint]%s failed:%d&#34;</span><span class="p">,</span> <span class="n">str</span><span class="p">,</span> <span class="n">WSAGetLastError</span><span class="p">());</span>
    <span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><h3 id="客户端的封装">客户端的封装</h3>
<h4 id="tcp_socket_clienth">TCP_SOCKET_CLIENT.h</h4>
<blockquote>
<p>增加了利用域名查询ip地址的成员函数gethostbyname(),挺好玩的！此次增加了erro_die函数，且发送和接收都操作套接字。在类的内部还是保留了套接字的备份，用于忘记关闭套接字时，析构函数进行关闭。</p>
</blockquote>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="c1">//
</span><span class="c1">// Created by Alone on 2021/8/18.
</span><span class="c1">//
</span><span class="c1"></span>
<span class="cp">#ifndef MY_TINY_STL_TCP_SOCKET_CLIENT_H
</span><span class="cp">#define MY_TINY_STL_TCP_SOCKET_CLIENT_H
</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&#34;TCP_INTERFACE.h&#34;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp"></span>
<span class="k">class</span> <span class="nc">TCP_SOCKET_CLIENT</span> <span class="o">:</span> <span class="k">public</span> <span class="n">TCP_INTERFACE</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
    <span class="n">TCP_SOCKET_CLIENT</span><span class="p">();</span>

    <span class="o">~</span><span class="n">TCP_SOCKET_CLIENT</span><span class="p">();</span>

    <span class="n">SOCKET</span> <span class="nf">Connect</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">IPAdrr</span><span class="p">,</span> <span class="n">u_short</span> <span class="n">port</span><span class="p">);</span>

    <span class="c1">//用于利用URL(域名)查询IP地址
</span><span class="c1"></span>    <span class="kt">void</span> <span class="nf">Gethostbyname</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">URL</span><span class="p">);</span>

    <span class="c1">//接口必须实现的函数
</span><span class="c1"></span>    <span class="kt">int</span> <span class="nf">Send</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">bufSize</span><span class="p">);</span>

    <span class="kt">int</span> <span class="nf">Recv</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span><span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">bufSize</span><span class="p">);</span>

    <span class="kt">void</span> <span class="nf">Close</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">);</span>

    <span class="kt">void</span> <span class="nf">error_die</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">);</span>

<span class="k">private</span><span class="o">:</span>
    <span class="c1">//由于一般客户端只需要一个套接字实现连接,然后还需要一个socketadrr_in用于连接内容的赋值
</span><span class="c1"></span>    <span class="n">SOCKET</span> <span class="n">clntSock</span><span class="p">;</span>
    <span class="n">sockaddr_in</span> <span class="n">sockAddr</span><span class="p">;</span>
<span class="p">};</span>


<span class="cp">#endif </span><span class="c1">//MY_TINY_STL_TCP_SOCKET_CLIENT_H
</span></code></pre></div><h4 id="tcp_socket_clientcpp">TCP_SOCKET_CLIENT.cpp</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="c1">//
</span><span class="c1">// Created by Alone on 2021/8/17.
</span><span class="c1">//
</span><span class="c1"></span>
<span class="cp">#include</span> <span class="cpf">&#34;TCP_SOCKET_CLIENT.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="c1">//初始化
</span><span class="c1"></span><span class="n">TCP_SOCKET_CLIENT</span><span class="o">::</span><span class="n">TCP_SOCKET_CLIENT</span><span class="p">()</span> <span class="o">:</span> <span class="n">clntSock</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="p">{}</span>

<span class="c1">//关闭套接字操作
</span><span class="c1"></span><span class="kt">void</span> <span class="n">TCP_SOCKET_CLIENT</span><span class="o">::</span><span class="n">Close</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">closesocket</span><span class="p">(</span><span class="n">clnt</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">error_die</span><span class="p">(</span><span class="s">&#34;close&#34;</span><span class="p">);</span>
    <span class="n">clntSock</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//连接服务器操作
</span><span class="c1"></span><span class="n">SOCKET</span> <span class="n">TCP_SOCKET_CLIENT</span><span class="o">::</span><span class="n">Connect</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">IPAdrr</span><span class="p">,</span> <span class="n">u_short</span> <span class="n">port</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sockAddr</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span> <span class="n">sockAddr</span><span class="p">);</span>
    <span class="n">clntSock</span> <span class="o">=</span> <span class="n">socket</span><span class="p">(</span><span class="n">PF_INET</span><span class="p">,</span> <span class="n">SOCK_STREAM</span><span class="p">,</span> <span class="n">IPPROTO_TCP</span><span class="p">);</span>
    <span class="n">sockAddr</span><span class="p">.</span><span class="n">sin_family</span> <span class="o">=</span> <span class="n">PF_INET</span><span class="p">;</span>
    <span class="n">sockAddr</span><span class="p">.</span><span class="n">sin_addr</span><span class="p">.</span><span class="n">s_addr</span> <span class="o">=</span> <span class="n">inet_addr</span><span class="p">(</span><span class="n">IPAdrr</span><span class="p">);</span>
    <span class="n">sockAddr</span><span class="p">.</span><span class="n">sin_port</span> <span class="o">=</span> <span class="n">htons</span><span class="p">(</span><span class="n">port</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">connect</span><span class="p">(</span><span class="n">clntSock</span><span class="p">,</span> <span class="p">(</span><span class="n">SOCKADDR</span> <span class="o">*</span><span class="p">)</span> <span class="o">&amp;</span><span class="n">sockAddr</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">sockAddr</span><span class="p">))</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">error_die</span><span class="p">(</span><span class="s">&#34;connect&#34;</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">clntSock</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//发送信息操作
</span><span class="c1"></span><span class="kt">int</span> <span class="n">TCP_SOCKET_CLIENT</span><span class="o">::</span><span class="n">Send</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">bufSize</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nf">send</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span> <span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">buf</span><span class="p">,</span> <span class="n">bufSize</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">//接收信息操作
</span><span class="c1"></span><span class="kt">int</span> <span class="n">TCP_SOCKET_CLIENT</span><span class="o">::</span><span class="n">Recv</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span><span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">bufSize</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nf">recv</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">buf</span><span class="p">,</span> <span class="n">bufSize</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">//根据域名获取ip地址等信息
</span><span class="c1"></span><span class="kt">void</span> <span class="n">TCP_SOCKET_CLIENT</span><span class="o">::</span><span class="n">Gethostbyname</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">URL</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">hostent</span> <span class="o">*</span><span class="n">host</span> <span class="o">=</span> <span class="n">gethostbyname</span><span class="p">(</span><span class="n">URL</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">host</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;Get IP address error!</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">;</span>
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="c1">//打印本命
</span><span class="c1"></span>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">URL</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="c1">//别名
</span><span class="c1"></span>    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">host</span><span class="o">-&gt;</span><span class="n">h_aliases</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&#34;Aliases %d: %s</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">host</span><span class="o">-&gt;</span><span class="n">h_aliases</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
    <span class="p">}</span>
    <span class="c1">//地址类型
</span><span class="c1"></span>    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;Address type: %s</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="p">(</span><span class="n">host</span><span class="o">-&gt;</span><span class="n">h_addrtype</span> <span class="o">==</span> <span class="n">AF_INET</span><span class="p">)</span> <span class="o">?</span> <span class="s">&#34;AF_INET&#34;</span> <span class="o">:</span> <span class="s">&#34;AF_INET6&#34;</span><span class="p">);</span>
    <span class="c1">//IP地址,其中inet_ntoa()函数是将网络字节序转为本地的字节序，方便打印看懂
</span><span class="c1"></span>    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">host</span><span class="o">-&gt;</span><span class="n">h_addr_list</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&#34;IP addr %d: %s</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">inet_ntoa</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="k">struct</span> <span class="nc">in_addr</span> <span class="o">*</span><span class="p">)</span> <span class="n">host</span><span class="o">-&gt;</span><span class="n">h_addr_list</span><span class="p">[</span><span class="n">i</span><span class="p">]));</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//析构时需要确保所有东西已经关闭
</span><span class="c1"></span><span class="n">TCP_SOCKET_CLIENT</span><span class="o">::~</span><span class="n">TCP_SOCKET_CLIENT</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">clntSock</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">closesocket</span><span class="p">(</span><span class="n">clntSock</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">TCP_SOCKET_CLIENT</span><span class="o">::</span><span class="n">error_die</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;[hint]%s failed:%d&#34;</span><span class="p">,</span> <span class="n">str</span><span class="p">,</span> <span class="n">WSAGetLastError</span><span class="p">());</span>
    <span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><h2 id="实例讲解">实例讲解</h2>
<h3 id="实例一回声程序通信">实例一：回声程序通信</h3>
<h4 id="服务器回声程序">服务器回声程序</h4>
<blockquote>
<p>绑定本地1234端口，进入监听状态等待请求，如果通信对象关闭了通信，也不慌，重新goto到等待请求得到新的通信套接字</p>
</blockquote>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&#34;TCP_SOCKET_SERVER.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="cp">#define BUF_SIZE 1000
</span><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">TCP_SOCKET_SERVER</span> <span class="n">a</span><span class="p">;</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Bind</span><span class="p">(</span><span class="mi">1234</span><span class="p">);</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Listen</span><span class="p">();</span>
    <span class="nl">restart</span><span class="p">:</span>
    <span class="n">SOCKET</span> <span class="n">clnt</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Accept</span><span class="p">();</span>
    <span class="k">while</span> <span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">char</span> <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">char</span><span class="p">[</span><span class="n">BUF_SIZE</span><span class="p">];</span>
        <span class="n">memset</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">BUF_SIZE</span><span class="p">);</span>
        <span class="kt">int</span> <span class="n">size</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Recv</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">BUF_SIZE</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">size</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">break</span><span class="p">;</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">Send</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">break</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Close</span><span class="p">(</span><span class="n">clnt</span><span class="p">);</span>
    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;connect is over.Waiting for a new connection!</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">;</span>
    <span class="k">goto</span> <span class="n">restart</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h4 id="客户端通信程序">客户端通信程序</h4>
<blockquote>
<p>为保持持续通信，一旦客户端拒绝了请求，那么弹出循环重新连接，并设置连接超时操作。</p>
</blockquote>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&#34;TCP_SOCKET_CLIENT.h&#34;</span><span class="cp">
</span><span class="cp">#define BUF_SIZE 100
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span>
    <span class="n">TCP_SOCKET_CLIENT</span> <span class="n">t</span><span class="p">;</span>
    <span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">to</span> <span class="o">=</span> <span class="s">&#34;127.0.0.1&#34;</span><span class="p">;</span>
    <span class="nl">restart</span><span class="p">:</span>
    <span class="n">SOCKET</span> <span class="n">clnt</span> <span class="o">=</span> <span class="n">t</span><span class="p">.</span><span class="n">Connect</span><span class="p">(</span><span class="n">to</span><span class="p">,</span><span class="mi">1234</span><span class="p">);</span>
    <span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&#34;</span><span class="se">\n</span><span class="s">Input your message:</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">;</span>
        <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">BUF_SIZE</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cin</span><span class="p">.</span><span class="n">getline</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">99</span><span class="p">);</span>
        <span class="kt">int</span> <span class="n">size</span> <span class="o">=</span> <span class="n">t</span><span class="p">.</span><span class="n">Send</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">BUF_SIZE</span><span class="p">);</span>
        <span class="k">if</span><span class="p">(</span><span class="n">size</span><span class="o">&lt;=</span><span class="mi">0</span><span class="p">)</span>
            <span class="k">break</span><span class="p">;</span>
        <span class="n">memset</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="k">sizeof</span> <span class="n">buf</span><span class="p">);</span>
        <span class="k">if</span><span class="p">(</span><span class="n">t</span><span class="p">.</span><span class="n">Recv</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">size</span><span class="p">)</span><span class="o">&lt;=</span><span class="mi">0</span><span class="p">)</span>
            <span class="k">break</span><span class="p">;</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&#34;received from %s is:</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">to</span><span class="p">);</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="n">buf</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">t</span><span class="p">.</span><span class="n">Close</span><span class="p">(</span><span class="n">clnt</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&#34;The Server is disconnected,and socket has been cleaned up,socket connection has been re-established</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">;</span>
    <span class="k">goto</span> <span class="n">restart</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h4 id="回声效果">回声效果</h4>
<blockquote>
<p>客户端收到的结果
<img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/4dde830c59194a30bb3fe5d62f45ef40.gif"
        data-srcset="https://img-blog.csdnimg.cn/4dde830c59194a30bb3fe5d62f45ef40.gif, https://img-blog.csdnimg.cn/4dde830c59194a30bb3fe5d62f45ef40.gif 1.5x, https://img-blog.csdnimg.cn/4dde830c59194a30bb3fe5d62f45ef40.gif 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/4dde830c59194a30bb3fe5d62f45ef40.gif"
        title="https://img-blog.csdnimg.cn/4dde830c59194a30bb3fe5d62f45ef40.gif" /></p>
</blockquote>
<blockquote>
<p>服务器端一直运行着，只要不关闭，但每次只能和一个客户端进行通信，通信完后重新等待连接。
<img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/f7ab866d1e604773a8559d0b3ad265bf.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        data-srcset="https://img-blog.csdnimg.cn/f7ab866d1e604773a8559d0b3ad265bf.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70, https://img-blog.csdnimg.cn/f7ab866d1e604773a8559d0b3ad265bf.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 1.5x, https://img-blog.csdnimg.cn/f7ab866d1e604773a8559d0b3ad265bf.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/f7ab866d1e604773a8559d0b3ad265bf.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        title="https://img-blog.csdnimg.cn/f7ab866d1e604773a8559d0b3ad265bf.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70" /></p>
</blockquote>
<h3 id="实例二文件操作传送图片掌握重复传包">实例二：文件操作，传送图片(掌握重复传包)</h3>
<h4 id="分析待传图片">分析待传图片</h4>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/583f25612beb4f5e8ef30422fafe41bc.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        data-srcset="https://img-blog.csdnimg.cn/583f25612beb4f5e8ef30422fafe41bc.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70, https://img-blog.csdnimg.cn/583f25612beb4f5e8ef30422fafe41bc.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 1.5x, https://img-blog.csdnimg.cn/583f25612beb4f5e8ef30422fafe41bc.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/583f25612beb4f5e8ef30422fafe41bc.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        title="https://img-blog.csdnimg.cn/583f25612beb4f5e8ef30422fafe41bc.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70" /></p>
<blockquote>
<p>看看这百万大小的字节，一次肯定是传不完的，所以我们需要发送端不断的续传，直到传送完毕。
<img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/7d8815d38ba74664b0543129b5b80d87.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        data-srcset="https://img-blog.csdnimg.cn/7d8815d38ba74664b0543129b5b80d87.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70, https://img-blog.csdnimg.cn/7d8815d38ba74664b0543129b5b80d87.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 1.5x, https://img-blog.csdnimg.cn/7d8815d38ba74664b0543129b5b80d87.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/7d8815d38ba74664b0543129b5b80d87.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        title="https://img-blog.csdnimg.cn/7d8815d38ba74664b0543129b5b80d87.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70" /></p>
</blockquote>
<h4 id="发送端程序">发送端程序</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&#34;TCP_SOCKET_CLIENT.h&#34;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span>
    <span class="n">TCP_SOCKET_CLIENT</span> <span class="n">t</span><span class="p">;</span>
    <span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">to</span> <span class="o">=</span> <span class="s">&#34;127.0.0.1&#34;</span><span class="p">;</span>
    <span class="nl">restart</span><span class="p">:</span>
    <span class="n">SOCKET</span> <span class="n">clnt</span> <span class="o">=</span> <span class="n">t</span><span class="p">.</span><span class="n">Connect</span><span class="p">(</span><span class="n">to</span><span class="p">,</span><span class="mi">1234</span><span class="p">);</span>
    <span class="c1">//图片写入buf(这几百万字节大小，得亏是new动态分配
</span><span class="c1"></span>    <span class="n">std</span><span class="o">::</span><span class="n">ifstream</span> <span class="n">img</span><span class="p">(</span><span class="s">&#34;D:/DesktopBackground/L-69.png&#34;</span><span class="p">,</span><span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">in</span><span class="o">|</span><span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">binary</span><span class="p">);</span>
        <span class="c1">//设置文件指针用于求文件内容长度
</span><span class="c1"></span>    <span class="n">img</span><span class="p">.</span><span class="n">seekg</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">end</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">len</span> <span class="o">=</span> <span class="n">img</span><span class="p">.</span><span class="n">tellg</span><span class="p">();</span>
    <span class="n">img</span><span class="p">.</span><span class="n">seekg</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">beg</span><span class="p">);</span>
    <span class="k">if</span><span class="p">(</span><span class="n">len</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">){</span><span class="n">printf</span><span class="p">(</span><span class="s">&#34;read OK</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);}</span>
    <span class="k">else</span> <span class="p">{</span><span class="n">printf</span><span class="p">(</span><span class="s">&#34;file is empty!&#34;</span><span class="p">);</span><span class="k">return</span> <span class="mi">0</span><span class="p">;}</span>
        <span class="c1">//填补buf
</span><span class="c1"></span>    <span class="kt">char</span> <span class="o">*</span> <span class="n">buf</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">char</span><span class="p">[</span><span class="n">len</span><span class="p">];</span>
    <span class="n">img</span><span class="p">.</span><span class="n">read</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="n">len</span><span class="p">);</span>
    <span class="c1">//发送数据到服务器,一次肯定发送不完，所以多次
</span><span class="c1"></span>    <span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">while</span><span class="p">(</span><span class="n">sum</span><span class="o">&lt;</span><span class="n">len</span><span class="p">){</span>
        <span class="kt">int</span> <span class="n">sendlen</span> <span class="o">=</span> <span class="n">t</span><span class="p">.</span><span class="n">Send</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">len</span><span class="p">);</span>
        <span class="k">if</span><span class="p">(</span><span class="n">sendlen</span><span class="o">&lt;=</span><span class="mi">0</span><span class="p">){</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&#34;Send Erro!&#34;</span><span class="p">);</span>
            <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="n">sum</span> <span class="o">+=</span> <span class="n">sendlen</span><span class="p">;</span>
    <span class="p">}</span>
	<span class="n">t</span><span class="p">.</span><span class="n">Close</span><span class="p">(</span><span class="n">clnt</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;Send OK!&#34;</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h4 id="接收端程序">接收端程序</h4>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&#34;TCP_SOCKET_SERVER.h&#34;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp">
</span><span class="cp">#define BUF_SIZE 100
</span><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">TCP_SOCKET_SERVER</span> <span class="n">a</span><span class="p">;</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Bind</span><span class="p">(</span><span class="mi">1234</span><span class="p">);</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Listen</span><span class="p">();</span>
    <span class="c1">//等待连接，连接成功便可建立通讯
</span><span class="c1"></span>    <span class="n">SOCKET</span> <span class="n">clnt</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Accept</span><span class="p">();</span>
    <span class="c1">//创建文件用于写入图片数据
</span><span class="c1"></span>    <span class="n">ofstream</span> <span class="n">t</span><span class="p">(</span><span class="s">&#34;test.png&#34;</span><span class="p">,</span><span class="n">ios</span><span class="o">::</span><span class="n">binary</span><span class="o">|</span><span class="n">ios</span><span class="o">::</span><span class="n">out</span><span class="p">);</span>
    <span class="c1">//由于要接收的图片文件较大，需要分多次包进行传输数据，所以需要不断循环接收
</span><span class="c1"></span>    <span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
        <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">BUF_SIZE</span><span class="p">];</span>
        <span class="kt">int</span> <span class="n">sz</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Recv</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">BUF_SIZE</span><span class="p">);</span>
        <span class="c1">//直到发送端发送数据完毕断开连接后，便可判断为接收完毕
</span><span class="c1"></span>        <span class="k">if</span><span class="p">(</span><span class="n">sz</span><span class="o">&lt;=</span><span class="mi">0</span><span class="p">){</span>
            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&#34;Finish !&#34;</span><span class="p">;</span>
            <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="c1">//每次调整文件指针位置到最后续写
</span><span class="c1"></span>        <span class="n">t</span><span class="p">.</span><span class="n">seekp</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">ios</span><span class="o">::</span><span class="n">end</span><span class="p">);</span>
        <span class="n">t</span><span class="p">.</span><span class="n">write</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="n">sz</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Close</span><span class="p">(</span><span class="n">clnt</span><span class="p">);</span>

<span class="p">}</span>
</code></pre></div><h4 id="接收结果">接收结果</h4>
<blockquote>
<p>一模一样毫无偏差
<img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/a3d39ffda4544aae9f48330b60398290.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        data-srcset="https://img-blog.csdnimg.cn/a3d39ffda4544aae9f48330b60398290.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70, https://img-blog.csdnimg.cn/a3d39ffda4544aae9f48330b60398290.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 1.5x, https://img-blog.csdnimg.cn/a3d39ffda4544aae9f48330b60398290.png?x-oss-process=image/watermark%2ctype_ZmFuZ3poZW5naGVpdGk%2cshadow_10%2ctext_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0%2csize_16%2ccolor_FFFFFF%2ct_70 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/a3d39ffda4544aae9f48330b60398290.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70"
        title="https://img-blog.csdnimg.cn/a3d39ffda4544aae9f48330b60398290.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUwOTQ1NTA0,size_16,color_FFFFFF,t_70" /></p>
</blockquote>
<h2 id="实例三web通信浏览器访问服务器">实例三：Web通信(浏览器访问服务器)</h2>
<h3 id="web服务器程序">Web服务器程序</h3>
<blockquote>
<p>我这个web服务器也算是及其简单了。。并没有对客户端的http请求进行解析然后发送对应的文件给客户端，而是单纯的我客户端想怎么发就怎么发。。另外这个程序虽然是对图片进行了判断，但并未写出对应的图片发送程序(二进制文件读写是不一样的)，所以实际只能发送文本文件，如html代码。所以后面看到的课程表都无法显示图片的原因是客户端程序压根就没在乎过客户端的请求🤣</p>
</blockquote>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&#34;TCP_SOCKET_SERVER.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="kt">void</span> <span class="nf">sendfileToWeb</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="n">TCP_SOCKET_SERVER</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">);</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">TCP_SOCKET_SERVER</span> <span class="n">a</span><span class="p">;</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Bind</span><span class="p">(</span><span class="mi">8086</span><span class="p">);</span>
    <span class="n">a</span><span class="p">.</span><span class="n">Listen</span><span class="p">();</span>
    <span class="n">SOCKET</span> <span class="n">clnt</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Accept</span><span class="p">();</span>
    <span class="k">while</span> <span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">sendfileToWeb</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="s">&#34;D:/Html/schedule/schedule.html&#34;</span><span class="p">);</span>
        <span class="n">a</span><span class="p">.</span><span class="n">Close</span><span class="p">(</span><span class="n">clnt</span><span class="p">);</span>
        <span class="n">clnt</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Accept</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="c1">//反馈请求，发送文件代码或者图片等二进制信息。
</span><span class="c1"></span><span class="kt">void</span> <span class="nf">sendfileToWeb</span><span class="p">(</span><span class="n">SOCKET</span> <span class="n">clnt</span><span class="p">,</span> <span class="n">TCP_SOCKET_SERVER</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">//写入返回头信息：包括状态和内容类型
</span><span class="c1"></span>    <span class="kt">char</span> <span class="o">*</span><span class="n">type</span> <span class="o">=</span> <span class="k">nullptr</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">strstr</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#34;.html&#34;</span><span class="p">))</span>
        <span class="n">type</span> <span class="o">=</span> <span class="s">&#34;text/html&#34;</span><span class="p">;</span>
    <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">strstr</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#34;.jpg&#34;</span><span class="p">))</span>
        <span class="n">type</span> <span class="o">=</span> <span class="s">&#34;image/jpg&#34;</span><span class="p">;</span>
    <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">strstr</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#34;.png&#34;</span><span class="p">))</span>
        <span class="n">type</span> <span class="o">=</span> <span class="s">&#34;image/png&#34;</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">x</span><span class="p">[</span><span class="mi">100</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
    <span class="n">sprintf</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s">&#34;HTTP/1.1 200 ok</span><span class="se">\r\n</span><span class="s">Content-Type: %s</span><span class="se">\r\n\r\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">type</span><span class="p">);</span>
    <span class="c1">//发送返回头信息,每次发送间隔需要一定时间，否则浏览器可能接收没这么快
</span><span class="c1"></span>    <span class="kt">int</span> <span class="n">sz1</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Send</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">x</span><span class="p">));</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">sz1</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span><span class="k">return</span><span class="p">;</span>
    <span class="c1">//发送文件内容到客户端
</span><span class="c1"></span>    <span class="n">std</span><span class="o">::</span><span class="n">ifstream</span> <span class="n">ss</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">in</span><span class="p">);</span>
    <span class="kt">char</span> <span class="n">html</span><span class="p">[</span><span class="mi">1024</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">ss</span><span class="p">.</span><span class="n">getline</span><span class="p">(</span><span class="n">html</span><span class="p">,</span> <span class="mi">1024</span><span class="p">))</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">szz</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">Send</span><span class="p">(</span><span class="n">clnt</span><span class="p">,</span> <span class="n">html</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">html</span><span class="p">));</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">szz</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="n">Sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="n">ss</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div><h3 id="接收结果-1">接收结果</h3>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/1e4a5a6b39b543679b8e5c7998ea79a3.gif"
        data-srcset="https://img-blog.csdnimg.cn/1e4a5a6b39b543679b8e5c7998ea79a3.gif, https://img-blog.csdnimg.cn/1e4a5a6b39b543679b8e5c7998ea79a3.gif 1.5x, https://img-blog.csdnimg.cn/1e4a5a6b39b543679b8e5c7998ea79a3.gif 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/1e4a5a6b39b543679b8e5c7998ea79a3.gif"
        title="https://img-blog.csdnimg.cn/1e4a5a6b39b543679b8e5c7998ea79a3.gif" /></p>
<h2 id="总结">总结</h2>
<p>收获：</p>
<ol>
<li>了解到网络通讯过程到底是怎么样的。</li>
<li>了解到底层socket通信是如何进行的。</li>
<li><strong>封装了socket操作，增强了代码的复用性。</strong></li>
<li><strong>对基本的http请求过程有所了解：</strong>
<strong>基本上就是浏览器(客户端)对相应的IP地址发起请求，其对应的服务器返回给你这个网页的主页，然后根据你鼠标的点击，又会触发http请求，其对应的服务器对你的请求进行解析，得出你想要的文件，然后发送给你，循环往复一直如此。。。</strong></li>
</ol>
</div><div class="post-footer" id="post-footer">
    <div class="post-info"><div class="post-info-tag"><span><a href="/tags/socket%E9%80%9A%E4%BF%A1/">socket通信</a>
                </span></div><div class="post-info-line"><div class="post-info-mod">
                <span>更新于 2022-01-20</span>
            </div><div class="post-info-mod"></div>
        </div><div class="post-info-share">
            <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-title="Socket基本操作的C&#43;&#43;封装" data-hashtags="socket通信"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-hashtag="socket通信"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-title="Socket基本操作的C&#43;&#43;封装" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-title="Socket基本操作的C&#43;&#43;封装"><i class="fab fa-line fa-fw"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-title="Socket基本操作的C&#43;&#43;封装" data-image="https://img-blog.csdnimg.cn/img_convert/c3d009d6542a44155b5847c3a9c76dcd.png#pic_center"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-title="Socket基本操作的C&#43;&#43;封装" data-description="”Socket基本操作的C&#43;&#43;封装“"><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-title="Socket基本操作的C&#43;&#43;封装" data-description="”Socket基本操作的C&#43;&#43;封装“"><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="https://acking-you.github.io/posts/socket%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9Cc&#43;&#43;%E5%B0%81%E8%A3%85/" data-title="Socket基本操作的C&#43;&#43;封装"><i class="fab fa-evernote fa-fw"></i></a></span>
        </div></div><div class="post-nav"><a href="/posts/%E5%A4%A7%E6%95%B4%E6%95%B0%E5%8A%A0%E5%87%8F/" class="prev" rel="prev" title="大数加减类的实现(C&#43;&#43;实现)✨"><i class="fas fa-angle-left fa-fw"></i>Previous Post</a>
            <a href="/posts/c&#43;&#43;%E7%9A%84%E6%A8%A1%E6%9D%BF%E5%86%99println/" class="next" rel="next" title="C&#43;&#43;变参模板运用实战——实现PrintLn">Next Post<i class="fas fa-angle-right fa-fw"></i></a></div></div>
</div></article></div>
            </main>
            <footer class="footer"><div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.86.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/khusika/FeelIt" target="_blank" rel="noopener noreffer" title="FeelIt 1.0.1"><i class="fas fa-hand-holding-heart fa-fw"></i> FeelIt</a>
        </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2023</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/"></a></span></div>
</div>
</footer>
        </div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-chevron-up fa-fw"></i>
            </a></div><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><script src="/lib/autocomplete/autocomplete.min.js"></script><script src="/lib/lunr/lunr.min.js"></script><script src="/lib/lunr/lunr.stemmer.support.min.js"></script><script src="/lib/lunr/lunr.zh.min.js"></script><script src="/lib/lazysizes/lazysizes.min.js"></script><script src="/lib/clipboard/clipboard.min.js"></script><script src="/lib/sharer/sharer.min.js"></script><script src="/lib/katex/katex.min.js"></script><script src="/lib/katex/auto-render.min.js"></script><script src="/lib/katex/copy-tex.min.js"></script><script src="/lib/katex/mhchem.min.js"></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":200},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":100,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script src="/js/theme.min.js"></script></body></html>
